package com.lc.dao.impl;

import com.lc.bean.Role;
import com.lc.bean.Users;
import com.lc.dao.DBUtils;
import com.lc.dao.UsersDao;
import com.lc.util.RoleEnum;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: ManolinCoder
 * @time: 2021/8/22
 */
public class UsersDaoImpl extends DBUtils implements UsersDao {
    @Override
    public Users login(String username, String password) {
        String sql = "select * from users where loginname=? and password=?";
        ArrayList arrayList = new ArrayList();
        arrayList.add(username);
        arrayList.add(password);
        resultSet = query(sql, arrayList);
        Users users = null;
        if (resultSet == null){
            return null;
        }
        try{
            while (resultSet.next()){
                users = new Users();
                users.setLoginName(username);
                users.setRealName(resultSet.getString("realname"));
                users.setUserId(resultSet.getInt("userid"));
                users.setRoleId(resultSet.getInt("roleid"));
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            //关闭的应当是父类中的全局变量，所以子类中要使用父类中的全局变量
            closeAll();
        }
        return users;
    }

    //查询用户列表
    @Override
    public List<Users> getUsersList(int pageIndex, int pageSize) {
        List<Users> usersList = new ArrayList<Users>();
        String sql = "select userid,loginname,realname,rolename from users u, role r where u.roleid=r.roleid limit ?,?";
        List params = new ArrayList();
        params.add((pageIndex-1)*pageSize);
        params.add(pageSize);
        resultSet = query(sql,params);
        try {
            while (resultSet.next()){
                //1.取出各表的数据
                Users users = new Users();
                users.setUserId(resultSet.getInt("userid"));
                users.setLoginName(resultSet.getString("loginname"));
                users.setRealName(resultSet.getString("realname"));
                Role role = new Role();
                role.setRoleName(resultSet.getString("rolename"));

                //2.建立关系
                users.setRole(role);

                usersList.add(users);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeAll();
        }
        return usersList;
    }

    //查询总条数
    @Override
    public int total() {
        int total = 0;
        String sql = "select count(*) from users u, role r where u.roleid=r.roleid";
        resultSet = query(sql,null);
        try {
            while (resultSet.next()){
                total = resultSet.getInt(1);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeAll();
        }
        return total;
    }

    //查询角色列表(角色状态值为1)

    @Override
    public List<Role> getRoleList() {
        List roles = new ArrayList();
        String sql = "select * from role";
        resultSet = query(sql,null);
        try {
            while (resultSet.next()){
                Role role = new Role();
                role.setRoleId(resultSet.getInt("roleid"));
                role.setRoleName(resultSet.getString("rolename"));
                role.setRoleState(resultSet.getInt("rolestate"));
                if (role.getRoleState()!=RoleEnum.UNAVAILABLE.type){
                    roles.add(role);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeAll();
        }
        return roles;
    }

    //新建角色
    @Override
    public int insertUser(Users users) {
        int i = 0;
        try {
            String sql = "insert into users values(null,?,?,?,?,?,?,?,?,?,?)";
            List params = new ArrayList();
            params.add(users.getLoginName());
            params.add(users.getPassWord());
            params.add(users.getRealName());
            params.add(users.getSex());
            params.add(users.getEmail());
            params.add(users.getAddress());
            params.add(users.getPhone());
            params.add(users.getCardId());
            params.add(users.getDesc());
            params.add(users.getRoleId());
            i = update(sql,params);//返回受影响的行数
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeAll();
        }
        return i;
    }
}
